library(dplyr)
library(readstata13)

main_final=readstata13::read.dta13("main_final.dta")

#2006
contract_06= main_final %>% filter(year==2006) 
contract_06$action_date=format(as.Date(contract_06$action_date),"%m/%d")
total06=data.frame(table(contract_06$action_date))
colnames(total06)=c("action_date","total")
nc06=data.frame(table(contract_06[contract_06$competition==0,]$action_date))
colnames(nc06)=c("action_date","nc")
nc_pres06=data.frame(table(contract_06[contract_06$competition==0&contract_06$inc==1,]$action_date))
colnames(nc_pres06)=c("action_date","nc_pres")

total06=merge(total06,nc06,by="action_date",all=T)
total06=merge(total06,nc_pres06,by="action_date",all=T)
total06$nc=ifelse(is.na(total06$nc),0,total06$nc)
total06$nc_pres=ifelse(is.na(total06$nc_pres),0,total06$nc_pres)

dev.off() 
par(mar=c(2,3.5,3,1))
plot(log(total06$total+1), main="2006", cex.main=1.8, type="l",col="grey",axes=F, xlab="",ylab="",cex=1.3);box()
#lines(log(total06$total+1), type="l",col="grey80",lty=1)
#lines(log(total06$nc+1), type="h",col=scales::alpha("red",0.7),lty=1)
lines(log(total06$nc+1), type="l",col=scales::alpha("red",0.7),lty=1)
lines(log(total06$nc_pres+1), type="l",col="blue",lty=1)
#lines(log(total06$nc_pres+1), type="h",col="blue",lty=1)
axis(side=1,at=1:nrow(total06), labels=total06$action_date,las=1,cex=3) #ADD X-AXIS LABELS WITH "YEAR-MONTH" FORMAT
axis(side=2,at=seq(0,10,by=1), labels=seq(0,10,by=1),las=2) #ADD Y-AXIS LABELS
#mtext('Date',side=1,line=3, cex=1.1) #X-AXIS LABEL
mtext('log(Contracts+1)',side=2,line=2, cex=1.7) #Y-AXIS LABEL

#2010
contract_10 = main_final %>% filter(year==2010)
contract_10$action_date=format(as.Date(contract_10$action_date),"%m/%d")
total10=data.frame(table(contract_10$action_date))
colnames(total10)=c("action_date","total")
nc10=data.frame(table(contract_10[contract_10$competition==0,]$action_date))
colnames(nc10)=c("action_date","nc")
nc_pres10=data.frame(table(contract_10[contract_10$competition==0&contract_10$inc==1,]$action_date))
colnames(nc_pres10)=c("action_date","nc_pres")

total10=merge(total10,nc10,by="action_date",all=T)
total10=merge(total10,nc_pres10,by="action_date",all=T)
total10$nc=ifelse(is.na(total10$nc),0,total10$nc)
total10$nc_pres=ifelse(is.na(total10$nc_pres),0,total10$nc_pres)

dev.off() 
par(mar=c(2,3.5,3,1))
plot(log(total10$total+1), main="2010", cex.main=1.5, type="l",col="grey",axes=F, xlab="",ylab="",cex=1.3);box()
#lines(log(total06$total+1), type="l",col="grey80",lty=1)
#lines(log(total06$nc+1), type="h",col=scales::alpha("red",0.7),lty=1)
lines(log(total10$nc+1), type="l",col=scales::alpha("red",0.7),lty=1)
lines(log(total10$nc_pres+1), type="l",col="blue",lty=1)
#lines(log(total06$nc_pres+1), type="h",col="blue",lty=1)
axis(side=1,at=1:nrow(total10), labels=total10$action_date,las=1,cex=2) #ADD X-AXIS LABELS WITH "YEAR-MONTH" FORMAT
axis(side=2,at=seq(0,10,by=1), labels=seq(0,10,by=1),las=2) #ADD Y-AXIS LABELS
#mtext('Date',side=1,line=3, cex=1.1) #X-AXIS LABEL
mtext('log(Contracts+1)',side=2,line=2, cex=1.3) #Y-AXIS LABEL

#2017
contract_17 = main_final %>% filter(year ==2017)
contract_17$action_date=format(as.Date(contract_17$action_date),"%m/%d")
total17=data.frame(table(contract_17$action_date))
colnames(total17)=c("action_date","total")
nc17=data.frame(table(contract_17[contract_17$competition==0,]$action_date))
colnames(nc17)=c("action_date","nc")
nc_pres17=data.frame(table(contract_17[contract_17$competition==0&contract_17$inc==1,]$action_date))
colnames(nc_pres17)=c("action_date","nc_pres")

total17=merge(total17,nc17,by="action_date",all=T)
total17=merge(total17,nc_pres17,by="action_date",all=T)
total17$nc=ifelse(is.na(total17$nc),0,total17$nc)
total17$nc_pres=ifelse(is.na(total17$nc_pres),0,total17$nc_pres)

dev.off()
par(mar=c(2,3.5,3,1))
plot(log(total17$total+1), main="2017", cex.main=1.8, type="l",col="grey",axes=F, xlab="",ylab="",cex=1.3);box()
#lines(log(total06$total+1), type="l",col="grey80",lty=1)
#lines(log(total06$nc+1), type="h",col=scales::alpha("red",0.7),lty=1)
lines(log(total17$nc+1), type="l",col=scales::alpha("red",0.7),lty=1)
lines(log(total17$nc_pres+1), type="l",col="blue",lty=1)
#lines(log(total06$nc_pres+1), type="h",col="blue",lty=1)
axis(side=1,at=1:nrow(total17), labels=total17$action_date,las=1,cex=3) #ADD X-AXIS LABELS WITH "YEAR-MONTH" FORMAT
axis(side=2,at=seq(0,10,by=1), labels=seq(0,10,by=1),las=2) #ADD Y-AXIS LABELS
#mtext('Date',side=1,line=3, cex=1.1) #X-AXIS LABEL
mtext('log(Contracts+1)',side=2,line=2, cex=1.7) #Y-AXIS LABEL

#2018
contract_18=main_final %>% filter(year==2018)
contract_18$action_date=format(as.Date(contract_18$action_date),"%m/%d")
total18=data.frame(table(contract_18$action_date))
colnames(total18)=c("action_date","total")
nc18=data.frame(table(contract_18[contract_18$competition==0,]$action_date))
colnames(nc18)=c("action_date","nc")
nc_pres18=data.frame(table(contract_18[contract_18$competition==0&contract_18$inc==1,]$action_date))
colnames(nc_pres18)=c("action_date","nc_pres")

total18=merge(total18,nc18,by="action_date",all=T)
total18=merge(total18,nc_pres18,by="action_date",all=T)
total18$nc=ifelse(is.na(total18$nc),0,total18$nc)
total18$nc_pres=ifelse(is.na(total18$nc_pres),0,total18$nc_pres)

dev.off() 
par(mar=c(2,3.5,3,1))
plot(log(total18$total+1), main="2018", cex.main=1.5, type="l",col="grey",axes=F, xlab="",ylab="",cex=1.3);box()
#lines(log(total06$total+1), type="l",col="grey80",lty=1)
#lines(log(total06$nc+1), type="h",col=scales::alpha("red",0.7),lty=1)
lines(log(total18$nc+1), type="l",col=scales::alpha("red",0.7),lty=1)
lines(log(total18$nc_pres+1), type="l",col="blue",lty=1)
#lines(log(total06$nc_pres+1), type="h",col="blue",lty=1)
axis(side=1,at=1:nrow(total18), labels=total18$action_date,las=1,cex=3) #ADD X-AXIS LABELS WITH "YEAR-MONTH" FORMAT
axis(side=2,at=seq(0,10,by=1), labels=seq(0,10,by=1),las=2) #ADD Y-AXIS LABELS
#mtext('Date',side=1,line=3, cex=1.1) #X-AXIS LABEL
mtext('log(Contracts+1)',side=2,line=2, cex=1.3) #Y-AXIS LABEL

